package cn.ulc.socket.weapp.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.model.param.SortParam;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.UserFavoriteGoodsDTO;
import cn.ulc.socket.weapp.model.dto.UserFavoriteGoodsListDTO;
import cn.ulc.socket.weapp.model.param.UserFavoriteGoodsParam;
import cn.ulc.socket.weapp.service.UserFavoriteGoodsServ;
import cn.ulc.store.dao.user.UserFavoriteGoodsDao;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.user.UserFavoriteGoods;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * 模板分类
 */
@Service
@AllArgsConstructor
public class UserFavoriteGoodsServImpl extends BaseService implements UserFavoriteGoodsServ {
    private static final Log log = LogFactory.get();


    UserFavoriteGoodsDao userFavoriteGoodsDao;

    @Override
    public UserFavoriteGoodsListDTO findList(PaginationParam param) {
        WeixinUser loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        SortParam sortParam = new SortParam("createAt",SortParam.DESC);
        PageQuery pageQuery = initPageQuery(param,sortParam);
        condition.put("wxUserId",loginUser.getId());
        pageQuery.setParas(condition);
        userFavoriteGoodsDao.findListPage(pageQuery);
        return new UserFavoriteGoodsListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }


    @Override
    public UserFavoriteGoodsDTO save(UserFavoriteGoodsParam param) {
        WeixinUser loginUser = getCurrentUser();
        Company company = getCurrentCompany();
        UserFavoriteGoods goods = userFavoriteGoodsDao.findByGoodsId(param.getGoodsId(),loginUser.getId());
        if(goods!=null) throw new PlatformException("已存在我的收藏列表");
        UserFavoriteGoods entity = new UserFavoriteGoods();
        setBaseInfo(entity);
        entity.setGoodsId(param.getGoodsId());
        entity.setGoodsName(param.getGoodsName());
        entity.setMainImage(param.getMainImage());
        entity.setPrice(param.getPrice());
        entity.setWxUserId(loginUser.getId());
        entity.setCompanyId(company.getId());
        entity.setBarCode(param.getBarCode());
        userFavoriteGoodsDao.save(entity);
        //其他关联处理
        return new UserFavoriteGoodsDTO(entity);
    }

    @Override
    public Long deleteOne(Long id) {
        UserFavoriteGoods entity = userFavoriteGoodsDao.findOne(id);
        if(entity == null) throw new PlatformException("获取收藏商品信息失败");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        userFavoriteGoodsDao.save(entity);
        //其他关联处理
        return id;
    }

}
